import { Controller,Post, Body,UseGuards,Request,UsePipes,HttpCode } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ValidationPipe } from 'src/pipe/validation/validation.pipe';
import { AuthService } from '../auth/auth.service';
import { RegisterInfoDTO,LoginInfoDTO } from './user.dto'; // 引入 DTO
import { UserService } from './user.service';
import { ApiTags,ApiBearerAuth,ApiBody,ApiOperation  } from '@nestjs/swagger';

@ApiBearerAuth() // Swagger 的 JWT 验证
@ApiTags('用户') // 添加 接口标签 装饰器 
@Controller('user')
export class UserController {
    constructor(private readonly authService:AuthService, private readonly userService: UserService){}

    @Post('find-one')
    findOne(@Body() body:any){
        return this.userService.findOne(body.phone);
    }
    

    // 注册
    @UsePipes(new ValidationPipe()) //使用管道
    @Post('register')
    @HttpCode(200)
    @ApiOperation({summary:'注册',description:'注册描述'}) //接口描述
    async register(@Body() body:RegisterInfoDTO){
        return await this.userService.register(body);
    }

    // JWT验证-用户请求登录
    @UsePipes(new ValidationPipe())
    @Post('login')
    @HttpCode(200)
    @ApiBody({
        description:'用户登录',
        type:LoginInfoDTO
    })
    async login(@Body() body:LoginInfoDTO){
        console.log('JWT验证 - Step 1: 用户请求登录');
        const authReult = await this.authService.validateUser(body.phone,body.pwd)
        if(authReult.code==1){
            return this.authService.certificate(authReult.user)
        }else{
            return {
                code:0,
                msg:authReult.msg||'用户不存在或密码错误'
            }
        }
    }


    // TOKEN守卫校验
    @UseGuards(AuthGuard('jwt'))
    @Post('check')
    @HttpCode(200)
    async check(@Body() body:any,@Request() req:any){
        console.log(req.user,'req===----')
        return {
            code:1,
            msg:'校验成功'
        }
    }
}
